package com.xiaochong.tir.manager.controller;

import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.Program;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.ProgramService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;

/**
 * @Author: WangXing
 * @Description:
 * @CreateDate: 2018/8/2 16:02
 */
@RestController
@RequestMapping(ManagerVersion.API_V1_PREFIX + "pro/")
@Api("App栏目管理")
public class ProgramController {

    @Autowired
    private ProgramService programService;

    @Autowired
    private OSSTool ossTool;
    @Autowired
    private RedisServer redisServer;

    /**
     * APP栏目管理页面
     * @return
     */
    @GetMapping("proPage")
    public Object programPage(){
        return new ModelAndView("program/programPage");
    }

    /**
     * 查询App栏目列表
     * @param pro
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "查询App栏目列表",notes = "查询App栏目列表")
    @PostMapping("proList")
    public Object programList(Program pro,
                              @RequestParam("page")Integer pageIndex,
                              @RequestParam("limit")Integer pageSize){
        PageInfo<Program> info = programService.selectByProList(pro,pageIndex,pageSize);
        for (int i = 0; i < info.getList().size(); i++) {
            Program program = info.getList().get(i);
            if(null != program){
                if(StringUtils.isNotBlank(program.getColumnImg())){
                    program.setColumnImg(ossTool.getOSSUrl(program.getColumnImg()));
                }
            }
        }
        return new ReturnResult<Program>(0,"成功",info.getList(),info.getTotal());
    }

    /**
     * 新增方法
     * @param pro
     * @return
     */
    @ApiOperation(value = "新增App栏目",notes = "新增App栏目")
    @PostMapping("addPro")
    public Object insPro(Program pro){
        redisServer.set(RedisKeysUtils.LOGINNAME,"新增App栏目");
        Program old = programService.selectByEntity(pro);
        if(null!= old){
            return new ModelAndView("program/programPage");
        }
        pro.setCreateTime(new Date());
        int count = programService.insertSelective(pro);
        return new ModelAndView("program/programPage");
    }

    /**
     * 新增页面
     * @return
     */
    @GetMapping("addprogram")
    public Object addPro(){
        return new ModelAndView("program/addProPage");
    }

    /**
     * 修改App栏目状态
     * @param pro
     * @return
     */
    @ApiOperation(value = "修改App栏目状态",notes = "修改App栏目状态")
    @PostMapping("enablePro")
    public Object updatePro(Program pro){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改App栏目状态");
        if(null != pro){
            Program old = programService.selectByEntity(pro);
            if(StringUtils.isNotBlank(String.valueOf(pro.getEnableFlag()))&&pro.getEnableFlag()){
                pro.setEnableFlag(false);
            }else {
                pro.setEnableFlag(true);
            }
            pro.setUpdateTime(new Date());
            programService.updateProgramKey(pro,old);
        }
        return new ReturnResult<Program>(0,"成功",1,1);
    }

    /**
     * 修改页面
     * @param id
     * @return
     */
    @GetMapping("updateprogram")
    public Object updateProgram(@RequestParam("id")Integer id){
        ModelAndView mv = new ModelAndView("program/editProPage");
        Program pro = programService.selectByPrimaryKey(id);
        if(StringUtils.isNotBlank(pro.getColumnImg())){
            pro.setColumnImgKey(pro.getColumnImg());
            pro.setColumnImg(ossTool.getOSSUrl(pro.getColumnImg()));
        }
        mv.addObject("pro",pro);
        return mv;
    }

    /**
     * 修改App栏目
     * @param pro
     * @return
     */
    @ApiOperation(value = "修改App栏目",notes = "修改App栏目")
    @PostMapping("editPro")
    public Object editProgram(Program pro){
        redisServer.set(RedisKeysUtils.LOGINNAME,"修改App栏目");
        if(null != pro){
            Program old = programService.selectByPrimaryKey(pro.getId());
            pro.setUpdateTime(new Date());
            programService.updateProgramKey(pro,old);
        }
        return new ModelAndView("program/programPage");
    }
}
